package com.delivery.deliverysystem.controller;

import com.delivery.deliverysystem.common.ServerResponse;
import com.delivery.deliverysystem.exception.RException;
import com.delivery.deliverysystem.pojo.DAO.CompanyDTO;
import com.delivery.deliverysystem.pojo.DO.Company;
import com.delivery.deliverysystem.service.CommonService;
import com.delivery.deliverysystem.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 公司资料
 */
@RestController
@RequestMapping("/company")
@Api(tags = "公司资料接口")
@Slf4j
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CommonService commonService;

    @PostMapping("/createCompany")
    @ApiOperation(value = "创建公司资料",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse createCompany(@RequestBody CompanyDTO companyDTO){
        boolean company = false;
        try {
            company = companyService.createCompany(companyDTO);
        } catch (RException e) {
            return ServerResponse.createByError(e.getMsg());
        }
        if(company) return ServerResponse.createBySuccess(null);
        return ServerResponse.createByError("创建失败");
    }

    @PostMapping("/updateCompany")
    @ApiOperation(value = "编辑公司资料",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse updateCompany(CompanyDTO companyDTO,
                                        @RequestParam(value = "companyLogo", required = false) MultipartFile companyLogo){
        boolean company = false;
        try {
            String logoPath ;
            if(companyLogo==null||companyLogo.isEmpty()) logoPath = null;
            else logoPath = commonService.uploadFile(companyLogo,"companyLogo");
            company = companyService.updateCompany(logoPath,companyDTO);
        }catch (RException re){
            log.error("更新公司资料时出错：{}", companyDTO, re);
            return ServerResponse.createByError(re.getMsg());
        }catch (Exception e) {
            log.error("更新公司资料时出错：{}", companyDTO, e);
        }
        if(company) return ServerResponse.createBySuccess("更新成功");
        return ServerResponse.createByError("更新失败");
    }

    @GetMapping("/getCompanyById")
    @ApiOperation(value = "根据id获取公司资料",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getCompanyById(@RequestParam Integer companyId){
        if(companyId==null) return ServerResponse.createByError("id不能为空");
        Company companyById = companyService.getCompanyById(companyId);
        if(companyById==null){
            return ServerResponse.createByError("该公司不存在");
        }
        return ServerResponse.createBySuccess(companyById);
    }

    @PostMapping("/generateCode")
    @ApiOperation(value = "生成注册码",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse generateCode(@RequestParam(value = "companyLogo",required = false) MultipartFile companyLogo,
                                       @RequestParam("companyName") String companyName){
        if(StringUtils.isBlank(companyName)) return ServerResponse.createByError("公司名不能为空");
        Map<String, Object> code = null;
        try {
            String filePath ;
            if(companyLogo==null||companyLogo.isEmpty()) filePath  = null;
            else {
                filePath = commonService.uploadFile(companyLogo,"companyLogo");
            }
            code = companyService.generateCode(filePath,companyName);
        } catch (RException e) {
            e.printStackTrace();
        }
        return ServerResponse.createBySuccess(code);
    }

}
